<template>
  <Datalist 
:name="name"
            :excelHeader="excelHeader"
            :cate="cate"
            :cols="cols"
            skipExcelNumber="2" />
</template>
<script>
import Datalist from '@/components/DataList'
import { getUser } from '@/utils/auth'
import { get教师基本情况, get教师基本情况ByGH } from '@/api/教师基本情况'
import { get教学工作量 } from '@/api/教学工作量'
import linq from 'linq'
import { addJbqk, deleteJbqk, updateJbqk, getJbqk, bulkAddJbqks } from '@/api/基本情况.js'
import { unzipSync } from 'zlib'
export default {
  components: {
    Datalist
  },
  data() {
    return {
      cate: 'A2-1基本情况',
      name: '2.1 基本情况',
      excelHeader: { B3: '工号', C3: '姓名', D3: '电话号码', E3: '电子邮箱', F3: '科研成果' },
      cols: [
        { label: '教工号', prop: 'GH' },
        { label: '姓名', prop: 'XM' },
        { label: '民族', prop: 'XZ_MZ' },
        { label: '职务', prop: 'ZW' },
        { label: '职称', prop: 'ZYJSZW_MC' },
        { label: '学历', prop: 'XZ_XL' },
        { label: '性别', prop: 'XZ_XB' },
        { label: '出生日期', prop: 'CSRQ' },
        { label: '区号-电话号码', prop: 'DHHM' },
        { label: '电子邮箱', prop: 'DZYX' },
        { label: '兼课量(学时)', prop: 'JXGZL' },
        { label: '科研成果', prop: 'KYCG' }
      ],
      JXGZL: []
    }
  },

  methods: {
    getSummaries(list) {
      //
      const sums = []
      this.cols.forEach((column, index) => {
        sums[index] = ''
      })
      sums[0] = '合计'
      sums[7] = '人数:' + linq.from(list).distinct('$.GH').count()
      sums[8] = '少数民族:' + linq.from(list).where("$.XZ_MZ!='汉族'").count()
      sums[11] = '大学或以上:' + linq.from(list).where("$.XZ_XL!='专科以下'&&$.XZ_XL!='专科'").count()
      sums[12] = '女:' + linq.from(list).where("$.XZ_XB=='女'").count()
      sums[16] = '兼课量:' + linq.from(list).sum('$.JXGZL')
      sums[18] = '通过:' + linq.from(list).where("$.审核状态=='通过'").count()
      sums[18] += ',不通过:' + linq.from(list).where("$.审核状态=='不通过'").count()
      sums[18] += ',待审核:' + linq.from(list).where("$.审核状态=='待审核'").count()
      return sums
      //
    },
    // 别的页面不用复制此方法

    async  onChange(model, field, value) {
      //

      if (field == 'GH') {
        if (value == 'error') {
          console.log('error')
          model.GH = ''
          model.XM = ''
          model.XZ_MZ = ''
          model.ZW = ''
          model.ZYJSZW_MC = ''
          model.XZ_XL = ''
          model.XZ_XB = ''
          model.CSRQ = ''
          model.JXGZL = ''
        } else {
          var d1 = await get教师基本情况ByGH(this.$root.user.XXID, this.$root.user.CJPC, value)
          // var Gzl = await get教学工作量(this.$root.user.XXID, this.$root.user.CJPC)
          var num = linq.from(this.JXGZL).where(`$.GH == ${value}`).toArray()
          console.log(Boolean(num.length))
          num.length ? model.JXGZL = num[0].JXGZL + '' : model.JXGZL = '0'

          model.GH = d1[0].GH
          model.XM = d1[0].XM
          model.XZ_MZ = d1[0].XZ_MZ
          model.ZW = d1[0].ZW
          model.ZYJSZW_MC = d1[0].ZYJSZW_MC
          model.XZ_XL = d1[0].XZ_XL
          model.XZ_XB = d1[0].XZ_XB
          // model.JXGZL = d1[0].JXGZL
          model.CSRQ = d1[0].CSRQ
        }
      }
      //
    },
    async  bulkAdd(array) {
      await bulkAddJbqks({ Jbqks: array })
    },
    // onFetchData(list){
    //   
    //   var user = getUser()
    //   //var datas = await getJbqk(user.XXID, user.CJPC)
    //   new Promise(async (resolve,reject)=>{
    //   var Gzl = await get教学工作量(user.XXID, user.CJPC)
      
    //   // Gzl = linq.from(Gzl).where('$.教师性质 == "校内兼课" || $.教师性质 == "校外兼课"').toArray()
    //   linq.from(list).join(Gzl, e => e.GH, e => e.GH, (outer, inner) => {
    //     console.log(outer)
        
    //     outer.JXGZL = inner.JXGZL
    //   }).toArray()
    //    resolve();
    //   });
    //   //console.log(datas, Gzl)

    // },
    async fetchData() {
      var user = getUser()
      var datas = await getJbqk(user.XXID, user.CJPC)
      var Gzl = await get教学工作量(user.XXID, user.CJPC)
      this.JXGZL = Gzl
      // Gzl = linq.from(Gzl).where('$.教师性质 == "校内兼课" || $.教师性质 == "校外兼课"').toArray()
      linq.from(datas).join(Gzl, e => e.GH, e => e.GH, (outer, inner) => {
        console.log(outer)
        outer.JXGZL = inner.JXGZL
      }).toArray()

      console.log(datas, Gzl)

      return datas
    },
    check(array, list) {
      return new Promise(async(resolve, reject) => {
        try {
          var user = getUser()
          var 教工号 = await get教师基本情况(user.XXID, user.CJPC)

          var duplicates = linq.from(array).groupBy('$.GH').where('$.count()>1').toArray()
          duplicates.forEach(e => {
            linq.from(array).where(t => t.GH == e.key()).forEach(t => t.error = '录入数据存在相同的教工号')
          })
          // 是否已经存在数据
          var err = linq.from(array).select('$.GH').intersect(linq.from(list).select('$.GH').toArray()).toArray()
          linq.from(array).where(e => err.indexOf(e.GH) !== -1).forEach(e => e.error = e.error ? e.error + ';' : '' + '数据库中已经存在教工号')
          // 是否已经存在相同

          err = linq.from(array).select('$.GH').except(linq.from(教工号).select('$.GH').toArray()).toArray()
          linq.from(array).where(e => err.indexOf(e.GH) !== -1).forEach(e => e.error = e.error ? e.error + ';' : '' + '教师基本情况表不存在对应的教工号')
          // 把错误的教师姓名替代掉
          err = linq.from(array).select("$.GH+','+$.XM").except(linq.from(教工号).select("$.GH+','+$.XM").toArray()).toArray()
          linq.from(array).where(e => err.indexOf(e.GH + ',' + e.XM) !== -1)
            .forEach(e => {
              var d = linq.from(教工号).firstOrDefault(item => item.GH == e.GH)
              if (d) {
                e.XM = d.XM
                e.XZ_MZ = d.XZ_MZ
                e.ZW = d.ZW
                e.ZYJSZW_MC = d.ZYJSZW_MC
                e.XZ_XL = d.XZ_XL
                e.XZ_XB = d.XZ_XB
                e.CSRQ = d.CSRQ
              }
            })
          resolve()
        } catch (e) {
          reject(e)
        }
      })
    },

    add_check(model, list) {
      //

      var first = linq.from(list).where('$.是否删除 === 0').firstOrDefault(e => e.GH == model.GH)
      console.log(first)
      if (first) { return '已经存在对应教工号的领导' }
      //
    },
    onadd(model) {
      //
      var temp = JSON.parse(JSON.stringify(model))
      return new Promise(async(resolve, reject) => {
        try {
          console.log(JSON.stringify(temp))
          var model = await addJbqk(temp)
          resolve(model)
        } catch (e) {
          reject(e)
        }
      })
    },
    onupdate(model) {
      //
      var temp = JSON.parse(JSON.stringify(model))
      return new Promise(async(resolve, reject) => {
        console.log(JSON.stringify(temp))
        var model = await updateJbqk(temp)
        resolve(model)
      })
      //
    },
    ondelete(model) {
      var temp = JSON.parse(JSON.stringify(model))
      return new Promise(async(resolve, reject) => {
        await deleteJbqk(temp)
        resolve()
      })
    }
  }
}
</script>
<style>
</style>

